package com.ruoyi.web.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.entity.PromotionActivity;
import com.ruoyi.web.service.IPromotionActivityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-28
 */
@RestController
@RequestMapping("/admin/activity")
public class PromotionActivityController extends BaseController {

    private final IPromotionActivityService promotionActivityService;

    public PromotionActivityController(IPromotionActivityService promotionActivityServiceImpl) {
        this.promotionActivityService = promotionActivityServiceImpl;
    }

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(PromotionActivity promotionActivity) {
        startPage();
        List<PromotionActivity> list = promotionActivityService.selectPromotionActivityList(promotionActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PromotionActivity promotionActivity) {
        List<PromotionActivity> list = promotionActivityService.selectPromotionActivityList(promotionActivity);
        ExcelUtil<PromotionActivity> util = new ExcelUtil<PromotionActivity>(PromotionActivity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(promotionActivityService.selectPromotionActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PromotionActivity promotionActivity) {
        promotionActivity.setId(IdUtil.getSnowflakeNextId());
        if (promotionActivity.getCouponId() == null) {
            promotionActivity.setRelatedCouponFlag(1);
        }
        return toAjax(promotionActivityService.insertPromotionActivity(promotionActivity));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PromotionActivity promotionActivity) {
        if (promotionActivity.getCouponId() == null) {
            promotionActivity.setRelatedCouponFlag(1);
        }
        return toAjax(promotionActivityService.updateById(promotionActivity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('admin:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(promotionActivityService.deletePromotionActivityByIds(ids));
    }
}
